<?php
	 
	// Used to simulate MFC Lists

	class PowerTabArray {
		 
		 
		protected $m_array = array ();
		 
		function & DeSerialize (&$stream , $object) {
			 
			// read count
			$n = READ_WORD($stream);
			 
			for ($i = 0; $i < $n; ++$i) {
				$this->ReadClassInfo ($stream, $object, $i);
			}
			 
			return $this->m_array;
			 
		}
		
		function Serialize (&$stream, &$objectArray) {
			
			$size = sizeof($objectArray);
			
			WRITE_WORD ($stream,$size);
			
			for ($i = 0; $i < $size; ++$i) {
				$this->WriteClassInfo ($stream, $objectArray[$i]);
			}
		}
		
		function WriteClassInfo (&$stream, &$object) {
			
			if ( !$stream->IsObjectSet($object->GetMFCClassName()) ) {
				
				WRITE_WORD ($stream,0xffff);
				$stream->MapObject ($object->GetMFCClassName());
				
				WRITE_WORD ($stream,1); // write schema
				
				WRITE_CSTRING_SECTION ($stream,$object->GetMFCClassName());
				
			} else { // write object ID or'd with mask
				WRITE_WORD ($stream,$stream->GetObjectId($object->GetMFCClassName()));
			}
			
			$stream->MapObject ($object->GetMFCClassName());
			
			$object->DoSerialize($stream);
			
		}
		 
		function ReadClassInfo (&$stream, $object, $i) {
			 
			$header = READ_WORD($stream);
			
			 
			if ($header == 0xffff) {
				// Alright New Section if equals 0xffff
				
				$stream->MapObject ($object->GetMFCClassName());
				 
				$schema = READ_WORD ($stream);
				 
				if ($schema != 1) {
					echo "Failed at schema in ptb_read_items. $schema\n";
					exit;
				}
				 
				$name_read = READ_CSTRING_SECTION ($stream);
				if ($object->GetMFCClassName() != $name_read) {
					echo "Failed at Section Name Verification. Read $name_read expected something else\n";
					exit;
				}
				 
			} elseif ( $header != $stream->GetObjectId($object->GetMFCClassName()) ) {
				// existing class check
				
				echo "<br/>Invalid Class Index Received " . $stream->GetClassID($header) . " ($header)";
				echo " expected " . $stream->GetClassID ($stream->GetObjectId($object->GetMFCClassName())) . " (" . $stream->GetObjectId($object->GetMFCClassName()) . ")";
				echo "<br/>For " . $object->GetMFCClassName();
				
				exit;
			}
			 
			$stream->MapObject ($object->GetMFCClassName());
			
			$this->m_array[$i] = clone $object;
			$this->m_array[$i]->DoDeserialize($stream);
			 
			 
			 
			 
		}
		 
		 
		 
	}
	 
	 
?>

